RNNäøLSTMåŗę¬äøęÆäøę ·ēļ¼LSTMåŖęÆå¤äŗéåæéØļ¼ęäŗéæēęč®°åæēęŗå¶ļ¼ä¼ęÆRNNę“儽äøäŗćē°åØęē« äøęå°ēRNNé½ęÆē¹ęLSTMćå¦ęęÆNLPé¢åļ¼å°±ę“ęÆē¹ę对čÆåéå äŗattentionēLSTMćå¹¶äøļ¼åØtensorflowäøļ¼LSTMäøRNNåØē¼ēØäøēå·®č·åŖåØäøč”代ē ēéæåŗ¦ćå ę¤ļ¼åē»ęåäŗ3äøŖåŗäŗLSTMēå®éŖć
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
steps = np.linspace(0, np.pi*2, 100, dtype=np.float32)
x_np = np.sin(steps)
y_np = np.cos(steps)
plt.plot(steps, y_np, 'r-', label='target (cos)')
plt.plot(steps, x_np, 'b-', label='input (sin)')
plt.legend(loc='best')
plt.show()
TIME_STEP = 10 # rnn time step
INPUT_SIZE = 1 # rnn input size
CELL_SIZE = 32 # rnn cell size
LR = 0.02 # learning rate
tf.reset_default_graph()
tf_x = tf.placeholder(tf.float32, [None, TIME_STEP, INPUT_SIZE])
tf_y = tf.placeholder(tf.float32, [None, TIME_STEP, INPUT_SIZE])
#it's really important to form the habit to use name_scope, especially when you use lstm and rnn!!!
#with tf.name_scope("first lstm")
rnn_cell = tf.contrib.rnn.BasicLSTMCell(CELL_SIZE, forget_bias=0.2, state_is_tuple=True)
init_s = rnn_cell.zero_state(batch_size=1, dtype=tf.float32) # very first hidden state
outputs, final_s = tf.nn.dynamic_rnn(
rnn_cell, # cell you have chosen
tf_x, # input
initial_state=init_s, # the initial hidden state
time_major=False, # False: (batch, time step, input); True: (time step, batch, input)
)
outs2D = tf.reshape(outputs, [-1, CELL_SIZE]) # reshape 3D output to 2D for fully connected layer
net_outs2D = tf.layers.dense(outs2D, INPUT_SIZE)
outs = tf.reshape(net_outs2D, [-1, TIME_STEP, INPUT_SIZE]) # reshape back to 3D
loss = tf.losses.mean_squared_error(labels=tf_y, predictions=outs) # compute cost
train_op = tf.train.AdamOptimizer(LR).minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer()) # initialize var in graph
los=[]
y_real=[]
y_pred=[]
for step in range(200):
start, end = step * np.pi, (step+1)*np.pi # time range
# use sin predicts cos
steps = np.linspace(start, end, TIME_STEP)
x = np.sin(steps)[np.newaxis, :, np.newaxis] # shape (batch, time_step, input_size)
y = np.cos(steps)[np.newaxis, :, np.newaxis]
if 'final_s_' not in globals(): # first state, no any hidden state
feed_dict = {tf_x: x, tf_y: y}
else: # has hidden state, so pass it to rnn
feed_dict = {tf_x: x, tf_y: y, init_s: final_s_}
_,los_, pred_, final_s_ = sess.run([train_op,loss, outs, final_s], feed_dict) # train
if step%10==0:
print(los_)
los.append(los_)
pred_=pred_.reshape(-1,1)
for item in range(len(pred_)):
y_pred.append(pred_[item][0])
plt.plot(range(len(los)),los)
plt.xlabel("Iterations")
plt.ylabel("Loss")